// JavaScript Document

/*!
 * jQuery Cookie Plugin v1.3.1
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2013 Klaus Hartl
 * Released under the MIT license
 */
(function ($, document, undefined) {

    var pluses = /\+/g;

    function raw(s) {
        return s;
    }

    function decoded(s) {
        return unRfc2068(decodeURIComponent(s.replace(pluses, ' ')));
    }

    function unRfc2068(value) {
        if (value.indexOf('"') === 0) {
            // This is a quoted cookie as according to RFC2068, unescape
            value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
        }
        return value;
    }

    function fromJSON(value) {
        return config.json ? JSON.parse(value) : value;
    }

    var config = $.cookie = function (key, value, options) {

        // write
        if (value !== undefined) {
            options = $.extend({}, config.defaults, options);

            if (value === null) {
                options.expires = -1;
            }

            if (typeof options.expires === 'number') {
                var days = options.expires, t = options.expires = new Date();
                t.setDate(t.getDate() + days);
            }

            value = config.json ? JSON.stringify(value) : String(value);

            return (document.cookie = [
                encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
                options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                options.path    ? '; path=' + options.path : '',
                options.domain  ? '; domain=' + options.domain : '',
                options.secure  ? '; secure' : ''
            ].join(''));
        }

        // read
        var decode = config.raw ? raw : decoded;
        var cookies = document.cookie.split('; ');
        var result = key ? null : {};
        for (var i = 0, l = cookies.length; i < l; i++) {
            var parts = cookies[i].split('=');
            var name = decode(parts.shift());
            var cookie = decode(parts.join('='));

            if (key && key === name) {
                result = fromJSON(cookie);
                break;
            }

            if (!key) {
                result[name] = fromJSON(cookie);
            }
        }

        return result;
    };

    config.defaults = {};

    $.removeCookie = function (key, options) {
        if ($.cookie(key) !== null) {
            $.cookie(key, null, options);
            return true;
        }
        return false;
    };

})(jQuery, document);


jQuery( function ($) {

	$(' \
	<div id="show" class="demo-panel"> \
		<div class="customize act" style="left: -350px;"> \
			<div class="close_bg"><a title="close" class="close_btn skin_close" href="#"></a></div> \
				 \
			<div class="skin-panel"> \
					 \
				<div class="img-preview"> \
					<a href="#" rel="skins/business/custom.css" data-logo="skins/business/business.header-logo-regular.png" data-logo-bot="skins/business/business.bottom-bar-logo-regular.png" class="skin-img"> \
						<img src="presscore-demo/skin/business.jpg" width="90" height="90" alt="blue_dark_boxed" /><i></i> \
						<p>Business</p> \
					</a> \
				</div> \
	 \
			 \
				<div class="img-preview"> \
					<a href="#" rel="skins/green/custom.css" data-logo="skins/green/green.header-logo-regular.png" data-logo-bot="skins/green/green.bottom-bar-logo-regular.png" class="skin-img"> \
						<img src="presscore-demo/skin/green.jpg" width="90" height="90" alt="blue_dark_wide" /><i></i> \
						<p>Green</p> \
					</a> \
				</div> \
	 \
			 \
				<div class="img-preview"> \
					<a href="#" rel="skins/minimal/custom.css" data-logo="skins/minimal/minimal.header-logo-regular.png" data-logo-bot="skins/minimal/minimal.bottom-bar-logo-regular.png" class="skin-img"> \
						<img src="presscore-demo/skin/minimal.jpg" width="90" height="90" alt="blue_light_doxed" /><i></i> \
						<p>Minimal</p> \
					</a> \
				</div> \
	 \
			 \
				<div class="img-preview"> \
					<a href="#" rel="skins/organic/custom.css" data-logo="skins/organic/organic.header-logo-regular.png" data-logo-bot="skins/organic/organic.bottom-bar-logo-regular.png" class="skin-img"> \
						<img src="presscore-demo/skin/organic.jpg" width="90" height="90" alt="blue_light_wide" /><i></i> \
						<p>Organic</p> \
					</a> \
				</div> \
	 \
			 \
				<div class="img-preview act"> \
					<a href="#" rel="skins/red/custom.css" data-logo="skins/red/red.header-logo-regular.png" data-logo-bot="skins/red/red.bottom-bar-logo-regular.png" class="skin-img"> \
						<img src="presscore-demo/skin/red.jpg" width="90" height="90" alt="pure_dark_boxed" /><i></i> \
						<p>Red</p> \
					</a> \
				</div> \
	 \
			 \
				<div class="img-preview"> \
					<a href="#" rel="skins/retro/custom.css" data-logo="skins/retro/retro.header-logo-regular.png" data-logo-bot="skins/retro/retro.bottom-bar-logo-regular.png" class="skin-img"> \
						<img src="presscore-demo/skin/retro.jpg" width="90" height="90" alt="pure_dark_wide" /><i></i> \
						<p>Retro</p> \
					</a> \
				</div> \
				<div><p><label id="make-box-label" for="make-box"><input type="checkbox" id="make-box" name="make-box"> Boxed</label></p></div> \
			</div><!-- .skin-panel --> \
		</div> \
	</div><!-- #show --> \
	').appendTo("body");


	if( $(".demo-panel").length > 0 ) {
		var loading_label = $('<div id="loading-overlay" class="loading-overlay" style="position: fixed" />').hide().appendTo("body");
	}
	else {
		return false;
	};

	if ($.cookie("css")) {

		$(".img-preview a").each(function() {
			if ( $(this).attr("rel") == $.cookie("css")) {
				$(".img-preview").removeClass("act");
				$(this).parent().addClass("act");
			};
		});

		if ($.cookie("is_boxed") == "true") {
			$("#make-box").attr("checked", "checked");
		}
		else {
			
		}
	}

	$(".img-preview a").click(function() {
		var $this = $(this);

		$(".img-preview").removeClass('act');
		$this.parent().addClass('act');

		$('.loading-overlay').fadeIn(300, function() {
			$("#skin-style").attr("href", $this.attr("rel"));
			$.cookie("css", $this.attr("rel"), {expires: 1, path: '/'});
	
			$('#branding img').attr("src", $this.attr("data-logo"));
			$.cookie("logo_top", $this.attr("data-logo"), {expires: 1, path: '/'});
	
			$('#branding-bottom img').attr("src", $this.attr("data-logo-bot"));
			$.cookie("logo_bot", $this.attr("data-logo-bot"), {expires: 1, path: '/'});
	
			$('.loading-overlay').delay(1000).fadeOut(200);
			return false;
		});
	});

	$("#make-box").click(function() {
		$("#page").toggleClass("boxed");
		$(window).trigger("resize");

		if ($.cookie("is_boxed") == "true") {
			$.cookie("is_boxed", false, {expires: 1, path: '/'});
		}
		else {
			$.cookie("is_boxed", true, {expires: 1, path: '/'});
		}
	});

	$(".demo-panel .skin_close").each(function(){
		$(this).click( function () {
			var $currentPanel = $(this).parents(".demo-panel"),
				$customize = $currentPanel.find(".customize");

			if ( parseInt( $customize.css("left") ) == 0 ) {
				$customize.addClass("act").animate(
					{
						left: "-350px"
					}, {
						duration: 500,
						complete: function () {
							$currentPanel.removeClass("panel-act");
						}
					}
				);
			}
			else {
				$currentPanel.addClass("panel-act");
				$customize.removeClass("act").animate({ left: "0px" }, { duration: 500, complete: function () {} } );
			}

			return false;
		});
	});

});